package com.china08.yxyapi.api.manage.sch.platform.website.template;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsTemplet;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingRepository;
import com.china08.yxyapi.repository.db.SwsSettingTempletRepository;

@CrossOrigin
@RestController("SwsSettingSrhTemplateController")
public class SwsTemplateSrhController extends YxyApiBaseController {

    @Autowired
    private SwsSettingTempletRepository swsSettingTempletRepository;

    @Autowired
    SwsSettingRepository swsSettingRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/template/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        String type = reqModel.type;
        String sort = reqModel.sort;
        boolean schoolIsVip = Boolean.parseBoolean(getProfile(request, response).getTemVip());
        String schoolId = getProfile(request, response).getSchoolId();

        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);

        Assert.isTrue(StringUtils.isNotBlank(type), "类型不能为空");
        Assert.isTrue(StringUtils.isNotBlank(sort), "分类不能为空");

        List<SwsTemplet> swsTempletList = swsSettingTempletRepository.findByStatusOrderByOrderNumAsc(1);
        List<SwsTemplet> swsTemplets = new ArrayList<>();
        RespModel respModel = new RespModel();
        if (null != swsTempletList) {
            SwsTemplet templet = null;
            for (SwsTemplet swsTemplet : swsTempletList) {
                templet = new SwsTemplet();
                templet.setId(swsTemplet.getId());
                templet.setName(swsTemplet.getName());
                templet.setType(swsTemplet.getType());
                templet.setSort(swsTemplet.getSort());
                templet.setIsVip(swsTemplet.getIsVip());
                templet.setTempletImage(swsTemplet.getTempletImage());
                swsTemplets.add(templet);
            }
        }
        respModel.swsTempletList = swsTemplets;
        respModel.skinTempl = swsSetting.getSkinTempl();
        respModel.schoolIsVip = schoolIsVip;
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String type;

        public String sort;

        public String name;

        public String templetImage;
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<SwsTemplet> swsTempletList;
        public String skinTempl;
        public boolean schoolIsVip = false;
    }
}
